﻿using CodeGenerator.Common;
using CodeGenerator.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator
{
    public partial class frmDBMySql : Form
    {
        public frmDBMySql()
        {
           
            InitializeComponent();
            
        }

        private void btnconnection_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbServer.Text))
            {
                Utils.MessageBoxShow("服务器不能为空!");
            }
            else if (string.IsNullOrEmpty(this.txtUserName.Text))
            {
                Utils.MessageBoxShow("登陆名不能为空!");
            }
            else
            {
                try
                {
                    IDbObject obj2 = new DbMySQLObject(false, this.cbbServer.Text, this.txtUserName.Text, this.txtPassword.Text, this.txtport.Text);
                    DataTable dBList = obj2.GetDBList();
                    this.cbbDatabase.Items.Clear();
                    this.cbbDatabase.Items.Add("全部");
                    foreach (DataRow row in dBList.Rows)
                    {
                        this.cbbDatabase.Items.Add(row[0].ToString());
                    }
                    this.cbbDatabase.Enabled = true;
                    this.cbbDatabase.SelectedIndex = 0;
                    Utils.MessageBoxShow("连接成功!");
                }
                catch (Exception exception)
                {
                    Utils.MessageBoxShow("连接失败!\n\r" + exception.Message);
                    this.cbbDatabase.Enabled = false;
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbServer.Text))
            {
                Utils.MessageBoxShow("服务器不能为空!");
            }
            else if (string.IsNullOrEmpty(this.txtUserName.Text))
            {
                Utils.MessageBoxShow("登陆名不能为空!");
            }
            else
            {
                IDbObject obj2 = new DbMySQLObject(false, this.cbbServer.Text, this.txtUserName.Text, this.txtPassword.Text, this.txtport.Text);
                string dbConnectStr = obj2.DbConnectStr;
                try
                {
                    using (MySqlConnection connection2 = new MySqlConnection(dbConnectStr))
                    {
                        connection2.Open();
                    }
                }
                catch (Exception exception)
                {
                    Utils.MessageBoxShow("连接失败!\n\r" + exception.Message);
                    return;
                }
                Connection conection = new Connection
                {
                    Database = (this.cbbDatabase.SelectedIndex == 0) ? "all" : this.cbbDatabase.Text,
                    ID = Guid.NewGuid()
                };
                conection.Name = this.cbbServer.Text + "(MySql)[" + conection.Database + "]";
                conection.ConnectionString = dbConnectStr;
                conection.DbType = DatabaseType.MySql.ToString();
                Utils.AddConnection(conection);
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void frmDBMySql_Load(object sender, EventArgs e)
        {
            this.cbbDatabase.Items.AddRange(new string[] { "全部" });
            this.cbbDatabase.SelectedIndex = 0;
        }
    }
}
